/**
*      author: Y_Yao
*      created: 2025-02-07 22:33:54
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 1e18;
const int INF = 0x3f3f3f3f;
const int N = 8 + 1;
int b[N];
int n,k;
vector<vector<int>> ans;

void bfs(int i,int len,int sum,vector<int>& a)
{
    if(len >= n)
    {
        if(sum % k == 0)
        {
            ans.push_back(a);
        }
    }

    for(int j = 1;j <= b[i];j++)
    {
        a.push_back(j);
        bfs(i + 1,len + 1,sum + j,a);
        a.pop_back();
    }
}

void solve()
{
    cin >> n >> k;
    for(int i = 1;i <= n;i++)
    {
        cin >> b[i];
    }

    vector<int> a;
    bfs(1,0,0,a);
    for(auto& e : ans)
    {
        for(auto& v : e)
        {
            cout << v << " ";
        }
        cout << endl;
    }
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}